
package com.coolgor.coldot.service.stock.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.stock.StockFollower;
import com.coolgor.coldot.dao.stock.StockFollowerDao;
import com.coolgor.coldot.dto.stock.StockFollowerExecution;
import com.coolgor.coldot.enums.stock.StockFollowerOperationStateEnum;
import com.coolgor.coldot.exceptions.stock.StockFollowerOperationException;
import com.coolgor.coldot.service.stock.StockFollowerService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class StockFollowerServiceImpl implements StockFollowerService {

	private static Logger log = LoggerFactory.getLogger(StockFollowerServiceImpl.class);

	@Autowired
	StockFollowerDao stockFollowerDao;

	@Override
	public StockFollowerExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL_ID);
		}
		StockFollower stockFollower = stockFollowerDao.queryByID(recordID);
		if (stockFollower != null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS, stockFollower);
		} else {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		}
	}

	@Override
	public StockFollowerExecution getList(StockFollower stockFollowerCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<StockFollower> list = stockFollowerDao.queryList(stockFollowerCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = stockFollowerDao.queryCount(stockFollowerCondition);
		StockFollowerExecution result = new StockFollowerExecution();
		if (list != null) {
			result.setState(StockFollowerOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(StockFollowerOperationStateEnum.SUCCESS.getStateInfo());
			result.setStockFollowerList(list);
			result.setCount(count);
		} else {
			result.setState(StockFollowerOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public StockFollowerExecution add(StockFollower stockFollower) throws StockFollowerOperationException {
		// 空值判断
		if (stockFollower == null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			stockFollower.setCreateTime(new Date());
			stockFollower.setLastEditTime(new Date());
			// 执行插入数据操作 
			effectedNum = stockFollowerDao.insert(stockFollower);
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower信息插入失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Add stockFollower error:" + e.getMessage());
		}
		return new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS, stockFollower);
	}

	@Override
	@Transactional
	public StockFollowerExecution modify(StockFollower stockFollower) throws StockFollowerOperationException {
		// 空值判断
		if (stockFollower == null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 把记录的最后更新时间设置为当前时间 
			stockFollower.setLastEditTime(new Date());
			// 执行修改更新操作 
			effectedNum = stockFollowerDao.update(stockFollower);
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower信息更新失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Update stockFollower error:" + e.getMessage());
		}
		return new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS, stockFollower);
	}

	@Override
	@Transactional
	public StockFollowerExecution remove(Long recordID) throws StockFollowerOperationException{
		// 空值判断
		if (recordID == null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockFollowerDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Delete stockFollower error:" + e.getMessage());
		}
		return new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public StockFollowerExecution remove(StockFollower stockFollower) throws StockFollowerOperationException{
		// 空值判断
		if (stockFollower == null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockFollowerDao.delete(stockFollower.getRecordID());
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Delete stockFollower error:" + e.getMessage());
		}
		return new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS, stockFollower);
	}

	@Override
	@Transactional
	public StockFollowerExecution removeBatchByID(List<Long> recordIDList) throws StockFollowerOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockFollowerDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Batch Delete stockFollower error:" + e.getMessage());
		}
		StockFollowerExecution exe = new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public StockFollowerExecution removeBatch(List<StockFollower> stockFollowerList) throws StockFollowerOperationException{
		// 空值判断
		if (stockFollowerList == null) {
			return new StockFollowerExecution(StockFollowerOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<stockFollowerList.size(); i++) {
			recordIDList.add(stockFollowerList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockFollowerDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockFollowerOperationException("StockFollower多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockFollowerOperationException("Batch Delete stockFollower error:" + e.getMessage());
		}
		StockFollowerExecution exe = new StockFollowerExecution(StockFollowerOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
